description "Datadog System Probe"

start on starting datadog-agent
stop on (runlevel [!2345] or stopping datadog-agent)

respawn
respawn limit 10 5
normal exit 0

console output

pre-start script
  # Do not run if config file doesn't exist
  test -f <%= etc_dir %>/system-probe.yaml || { stop ; exit 0; }

  # Manual rotation of errors log
  log_file_size=`du -b /var/log/datadog/system-probe-errors.log | cut -f1`
  if [ -n "$log_file_size" ] && [ $log_file_size -gt 5242880 ]; then
    # Rotate log file if it's larger than 5MB
    mv /var/log/datadog/system-probe-errors.log /var/log/datadog/system-probe-errors.log.1
  fi

  # mount debugfs before start
  if [ -d /sys/kernel/debug ]; then
    if ! grep -qs '/sys/kernel/debug ' /proc/mounts; then
      mount -t debugfs none /sys/kernel/debug
    fi
  fi
end script

script
  # Logging to console from the agent is disabled since the agent already logs using file or
  # syslog depending on its configuration. We then redirect the stdout/stderr of the agent process
  # to log panic/crashes.
  exec <%= install_dir %>/embedded/bin/system-probe --config=<%= etc_dir %>/system-probe.yaml --pid=<%= install_dir %>/run/system-probe.pid &>> /var/log/datadog/system-probe-errors.log
end script

post-stop script
  rm -f <%= install_dir %>/run/system-probe.pid
end script
